package cn.itrip.auth.controller;

import cn.itrip.auth.service.UserService;
import cn.itrip.beans.dto.Dto;
import cn.itrip.beans.pojo.ItripUser;
import io.swagger.annotations.ApiOperation;
import org.springframework.mobile.device.Device;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * Created by Administrator on 2018/11/23.
 */
@RequestMapping("/api")
@RestController
public class RegeditController {
    @Resource
    private UserService userService;
    /**
     * 登录
     */
    @ApiOperation(value="登录",httpMethod = "POST", protocols = "HTTP", produces = "application/json", response = Dto.class,notes="用户登录")
    @RequestMapping(value="/dologin",method = RequestMethod.POST)
    public Dto login(String name, String password, Device device) throws Exception {
        return  userService.selectByLogin(name, password,device.getDevicePlatform().name());
    }

    /**
     * 置换token
     * @param request
     * @return
     */
    @RequestMapping(value="/retoken",method = RequestMethod.POST,headers="token")
    public Dto reloadToken(HttpServletRequest request) throws Exception {
        String token=request.getHeader("token");
        return userService.reloadToken(token);
    }
    /**
     *注销
     */
    @RequestMapping(value="/logout",method = RequestMethod.GET,headers = "token")
    public Dto logout(HttpServletRequest request) throws Exception {
        String token=request.getHeader("token");
        return userService.logout(token);
    }
    /**
     * 用户邮箱注册
     */
    @ApiOperation(value = "用户邮箱注册",notes = "邮箱注册",responseContainer = "Json",response = Dto.class)
    @RequestMapping(value = "/doregister",method = RequestMethod.POST)
    public Dto emailRege(@RequestBody ItripUser user) throws Exception {
        return userService.emailRegedit(user);
    }
    /**
     *  通过手机号码注册
     */
    @ApiOperation(value="手机短信验证API",httpMethod = "POST", protocols = "HTTP", produces = "application/json", response = Dto.class,notes="使用手机短信验证")
    @RequestMapping(value = "/registerbyphone",method = RequestMethod.POST)
    public Dto registerbyphone(@RequestBody ItripUser user) throws Exception {
        return userService.phoneRegedit(user);
    }
    /**
     *邮箱激活
     */
    @ApiOperation(value = "邮箱激活",httpMethod = "POST", protocols = "HTTP", produces = "application/json", response = Dto.class,notes="激活帐号")
    @RequestMapping(value="/activate" ,method = RequestMethod.PUT)
    public Dto activeMail(@RequestParam("code")String code, @RequestParam("user")String userCode) throws Exception {
        //激活
        return userService.updateActivated(userCode,code);
    }
    /**
     * 手机激活
     */
    @ApiOperation(value="验证手机验证码",httpMethod = "POST", protocols = "HTTP", produces = "application/json", response = Dto.class,notes="使用手机短信验证")
    @GetMapping(value = "/validatephone")
    public Dto validatephone(@RequestParam("code")String code, @RequestParam("user")String userCode) throws Exception{
        return userService.updateActivated(userCode,code);
    }
    /**
     * 判断该userCode是否已被注册
     */
    @ApiOperation(value="userCode存在验证",httpMethod = "POST", protocols = "HTTP", produces = "application/json", response = Dto.class,notes="判断userCode是否已存在")
    @RequestMapping(value="/ckusr" ,method = RequestMethod.GET)
    public  Dto ckusr(String name) throws Exception {
        return userService.selectByUserCode(name);
    }
}
